﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BugTracker.BusinessEntities.Entities.Company;
using BugTracker.BusinessLogic.Company;
using Microsoft.Practices.Unity;
using BugTracker.BusinessLogic.BTUser;
using BugTracker.BusinessLogic.Issue;
using BugTracker.BusinessLogic.Project;

namespace BugTracker.WebUX.Shared
{
    /// <summary>
    /// Summary description for FetchImage
    /// </summary>
    public class FetchImage : IHttpHandler
    {
        private const string defaultImgUrl = "~/Shared/Images/nopic.png";

        public void ProcessRequest(HttpContext context)
        {
            try
            {
                string p1 = context.Request.QueryString["p1"];
                string p2 = context.Request.QueryString["p2"];
                if (!string.IsNullOrEmpty(p1) & !string.IsNullOrEmpty(p2))
                {
                    int id1 = 0;
                    int id2 = 0;
                    Int32.TryParse(p1, out id1);
                    Int32.TryParse(p2, out id2);
                    if (id1 != 0 & id2 != 0)
                    {
                        byte[] img = null;

                        switch (id1)
                        {
                            case 1:
                                img = Global.IoC.Container.Resolve<ICompanyTypeService>().LoadImageById(id2);
                                break;
                            case 2:
                                img = Global.IoC.Container.Resolve<IUserTypeService>().LoadImageById(id2);
                                break;
                            case 3:
                                img = Global.IoC.Container.Resolve<IIssueTypeService>().LoadImageById(id2);
                                break;
                            case 4:
                                img = Global.IoC.Container.Resolve<IIssueStatusTypeService>().LoadImageById(id2);
                                break;
                            case 5:
                                img = Global.IoC.Container.Resolve<IIssueResolutionTypeService>().LoadImageById(id2);
                                break;
                            case 6:
                                img = Global.IoC.Container.Resolve<IIssuePriorityTypeService>().LoadImageById(id2);
                                break;
                            case 7:
                                img = Global.IoC.Container.Resolve<IPlatformTypeService>().LoadImageById(id2);
                                break;
                            case 8:
                                img = Global.IoC.Container.Resolve<IIssueSeverityTypeService>().LoadImageById(id2);
                                break;
                            case 9:
                                img = Global.IoC.Container.Resolve<ICompanyService>().LoadImageById(id2);
                                break;
                        }

                        if (img != null)
                        {
                            context.Response.ContentType = "image/jpeg";
                            context.Response.BinaryWrite(img);
                            context.Response.Flush();
                        }
                    }
                    else
                    {
                        string path = context.Server.MapPath(defaultImgUrl);
                        context.Response.ContentType = "image/png";
                        context.Response.WriteFile(path);
                        context.Response.Flush();
                    }
                }
            }
            catch { }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}